package sd.czw.com.mmgapp.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import com.zhy.http.okhttp.OkHttpUtils;
import java.util.List;
import okhttp3.Call;
import sd.czw.com.mmgapp.Contants;
import sd.czw.com.mmgapp.R;
import sd.czw.com.mmgapp.mcallback.MyGoodsDetatilRightCallback;
import sd.czw.com.mmgapp.model.goodsbean.GoodsdetailsRightModel;



/**
 * Created by czw
 * desc 描述：商品详情fragment
 */
public class GoodsDetailFragmentRight extends Fragment {

    public static GoodsDetailFragmentRight newInstance(String goods_id) {
        Bundle args = new Bundle();
        args.putString("goods_id",goods_id);
        GoodsDetailFragmentRight fragment = new GoodsDetailFragmentRight();
        fragment.setArguments(args);
        return fragment;
    }

    private View view;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragmen_goodsdetatils_right,container,false);
        //获取webview 控件
        final WebView webView = (WebView) view.findViewById(R.id.webview_goodsdetatil);
        //设置webView的属性
        setWebView(webView);
        //获取商品id
        String goods_id = getArguments().getString("goods_id");
        //在webview中加载HTML图片集合
        loadwebviewImage(webView, goods_id);
        return view;
    }

    //在webview中加载HTML图片集合
    private void loadwebviewImage(final WebView webView, String goods_id) {
        OkHttpUtils.post().url(Contants.GOODS_DETATIL).addParams("time",Contants.Time)
                .addParams("sign",Contants.Sign).addParams("act","detail").addParams("id",goods_id).build()
                .execute(new MyGoodsDetatilRightCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(GoodsdetailsRightModel response, int id) {
                        Integer errcode = response.getErrcode();
                        if (errcode == 1 && errcode != null){
                            List<GoodsdetailsRightModel.ErrmsgBean> errmsg = response.getErrmsg();
                            String html="";
                            for (int i = 0;i < errmsg.size();i++){
                                html=html+ "<img src="+errmsg.get(i).getImage()+" alt="+" />";
                            }
                            String css = "<style type=\"text/css\"> img {" +
                                    "width:100%;" +
                                    "height:auto;" +
                                    "}" +
                                    "body {" +
                                    "font-size:45px;" +
                                    "}" +
                                    "</style>";
                            html = "<html><header>" + css + "</header><body>" + html + "</body></html>";
                            webView.loadDataWithBaseURL(null, html, "text/html", "utf-8", null);
                        }
                    }
                });
    }

    //设置webView的属性
    private void setWebView(WebView webView) {
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setSupportZoom(true);//支持缩放
        webView.getSettings().setBuiltInZoomControls(true);//支持缩放
        webView.getSettings().setDisplayZoomControls(false);//控制缩放图标的显示隐藏
        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setJavaScriptEnabled(false);
    }
}
